import { NextRequest, NextResponse } from "next/server";
import prisma from "@/db";

export const GET = async () => {
  /* await prisma.categroy.create({
    data: {
    name: '手办~',
      desc: '这是手办描述~'
    }
  }) */
  const data = await prisma.goods.findMany({
    where: {},
    orderBy: {
      createdAt: "desc", // 创建时间倒叙
    },
    include: {
      // !关联查询
      category: true,
    }
  });
  return NextResponse.json({
    success: true,
    errorMessage: "",
    data,
  });
};

export const POST = async (request: NextRequest) => {
  const body = await request.json();
  await prisma.goods.create({
    data: {
      name: body.name,
      price: body.price,
      categoryId: body.categoryId
    },
  });
  return NextResponse.json({
    success: true,
    errorMessage: "保存成功",
  });
};

// http://localhost:3000/api/products?id=5939efca-77b3-4701-8af8-85a7448a4de7
export const DELETE = async (request: NextRequest) => {
  const { searchParams } = new URL(request.url);
  await prisma.goods.delete({
    where: {
      id: searchParams.get("id")!,
    },
  });
  return NextResponse.json({
    success: true,
    errorMessage: "删除成功",
  });
};
